define(function () {
    'use strict';

    function ctrl($scope,$sce,$ionicHistory,$ionicLoading,$timeout,LocalCacheService,InfoService, WebService,$ionicPopup) {

        var fund = $scope.fund = {};
        $scope.$on('$ionicView.loaded', function() {
            $scope.id = "1104";
            init();
        });

        function init(){
            WebService.getEncontract($scope.id).then(function (data){
                $scope.fund = data;
                $scope.fund.econtract_content=$sce.trustAsHtml(data.econtract_content);

            });
        }

        $scope.closeAgreementContent = function(){
            if(checkStep()){
                WebService.openElectronic().then(function (data){
                    if(data){
                        $ionicPopup.show({
                            cssClass: "kDialog",
                            template: "签署电子签名约定书成功！以后您将可直接签署协议！",
                            title: "提示",
                            buttons: [
                                { text: '继续购买',
                                    onTap: function(e) {
                                        $ionicHistory.goBack();
                                    }
                                }
                            ]
                        });
                    }else{
                        $ionicPopup.show({
                            cssClass: "kDialog",
                            template: "签约失败，请稍后再试！",
                            title: "提示",
                            buttons: [
                                { text: '我知道了'}
                            ]
                        });
                    }
                });
            }
        }

        function checkStep(){
            if(!$scope.fund.isChecked){//未选中
                $ionicPopup.show({
                    cssClass: "kDialog",
                    template: "您尚未勾选同意电子签名约定书内容，若继续签署请阅读并勾选“我已阅读并同意以上内容”！",
                    title: "提示",
                    buttons: [
                        { text: '我知道了'}
                    ]
                });
                return false;
            }else{
                return true;
            }
        }
    }

    ctrl.$inject = ['$scope','$sce','$ionicHistory','$ionicLoading','$timeout','LocalCacheService','InfoService', 'WebService','$ionicPopup'];
    return ctrl;
});
